package com.huangguolin.cloud.login.control;


import com.huangguolin.cloud.login.service.LoginService;
import com.huangguolin.cloud.mybatis.entity.PtRoleEntity;
import com.huangguolin.cloud.mybatis.entity.PtUserEntity;
import com.huangguolin.cloud.mybatis.flexigrid.PadingRstEntity;
import com.huangguolin.cloud.spring.control.BaseController;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/login")
public class LoginController extends BaseController {
    @Resource
    HttpSession session = null;
    @Resource
    private LoginService loginService = null;

    @Value("${error.num}")
    private Integer errorNum;//密码错误次数

    private Logger logger = LogManager.getLogger(this.getClass());

    @RequestMapping("/login")
    public String loginPage(HttpServletRequest request){
        String lang = LocaleContextHolder.getLocale().toString();
        HttpSession session = request.getSession();
        session.setAttribute("lang",lang);
        return "login/loginPage";
    }

    @RequestMapping("/main")
    public String mainPage(){

        return "redirect:/";
    }

    @RequestMapping("/getVerifyCodeImg")
    @ResponseBody
    public ResponseEntity<byte[]> getVerifyCodeImg(){//获取登录页面验证码

        ResponseEntity<byte[]> responseEntity = loginService.getVerifyCodeImg();
        return responseEntity;
    }

    @RequestMapping("/authVerifyCode")
    @ResponseBody
    public String authVerifyCode(@RequestParam(value = "verifyCode",required = false)String verifyCode){//验证码比较
        logger.info("verifyCode:"+verifyCode);
        Object verifyCodeSession = session.getAttribute("verifyCode");
        do {
            if (verifyCode == null){
                break;
            }
            if (verifyCode.equals(verifyCodeSession)){
                return successJSON("login.verify.success");
            }
        }while (false);
        return failJSON("login.verify.fail");

    }
    @RequestMapping("/forgetPage")
    public String forgetPage(HttpServletRequest request){
        String lang = LocaleContextHolder.getLocale().toString();
        HttpSession session = request.getSession();
        session.setAttribute("lang",lang);
        return "login/forgetPage";
    }

    @RequestMapping("/regPage")
    public String regPage(HttpServletRequest request){
        String lang = LocaleContextHolder.getLocale().toString();
        HttpSession session = request.getSession();
        session.setAttribute("lang",lang);
        return "login/regPage";
    }



//    @RequestMapping("/sendemailcode")
//    @ResponseBody
//    public String sendemailcode(@RequestParam(value = "emailCode",required = false)String emailCode){
//        boolean rst = loginService.queryUserIdByEmail(emailCode);
//
//        if (rst){
//            loginService.getemailcode(emailCode);
//            String code= (String) session.getAttribute("code");
//            return successJSON("login.sendemailcode.success");
//        }else{
//            return failJSON("login.sendemailcode.fail");
//        }
//
//    }

    @RequestMapping("/sendemailregcode")
    @ResponseBody
    public String sendemailregcode(@RequestParam(value = "emailCode",required = false)String emailCode){
        boolean rst = loginService.queryUserIdByEmail(emailCode);

        if (!rst){
            loginService.getemailcode(emailCode);
            String code= (String) session.getAttribute("code");
            return successJSON("login.sendemailcode.success");
        }else{
            return failJSON("login.sendemailregcode.fail");
        }
    }

    @RequestMapping("/authEmailCode")
    @ResponseBody
    public String authEmailCode(@RequestParam(value = "emailverifyCode",required = false)String emailverifyCode){
        logger.info("emailverifyCode:"+emailverifyCode);
        String code= (String) session.getAttribute("code");
        do {
            if (emailverifyCode == null){
                break;
            }
            if (emailverifyCode.equals(code)){
                return successJSON("login.email.verify.success");
            }
        }while (false);
        return failJSON("login.email.verify.fail");
    }


    @RequestMapping("/loginError")
    public String loginError(){
       Integer errorNumSession = (Integer) session.getAttribute("errorNumSession");
       if (errorNumSession == null){
           errorNumSession = 0;
       }
       errorNumSession++;
       session.setAttribute("errorNumSession",errorNumSession);
       if (errorNumSession >= errorNum){
           session.setAttribute("authFlag","1");
       }
       return "login/loginPage";
    }

    /**
     * 重置密码
     * @param password1 新密码
     * @return
     */
    @RequestMapping("/resttingPassword")
    @ResponseBody
    public String resttingPassword(@RequestParam(value = "password1",required = false)String password1){
        loginService.resttingPassword(password1);
        return successJSON("login.email.restting.success");
    }

    /**
     * 注册新用户
     * @param password2
     * @param emailCode
     * @return
     */
    @RequestMapping("/registerUser")
    @ResponseBody
    public String registerUser(@RequestParam(value = "password2",required = false)String password2,@RequestParam(value = "emailCode",required = false)String emailCode,@RequestParam(value = "regniceName",required = false)String regniceName,@RequestParam(value = "regName",required = false)String regName ){
        loginService.registerUser(password2,emailCode,regName,regniceName);
        return successJSON("login.register.success");
    }

    /**
     * 校验用户名
     * @param regName
     * @return
     */
    @RequestMapping("/infoUserName")
    @ResponseBody
    public  String infoUserName(String regName){
        boolean rst  = loginService.infoUserName(regName);

        if (rst){
            return failJSON("login.regname.fail");
        }else{
            return successJSON("login.regname.success");

        }
    }

}
